package com.test.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.model.Plupload;
import com.test.service.PluploadService;

@Controller
@RequestMapping(value = "/uploader")
public class UploaderController {

	@Autowired
	private PluploadService pluploadService;

	@RequestMapping(value = "/index")
	public String index() {
		return "index";
	}

	/**
	 * 上传
	 * 
	 * @return
	 */
	@RequestMapping(value = "/upload")
	@ResponseBody
	public Map<String, Object> upload(Plupload plupload, HttpServletRequest request, HttpServletResponse response) {

		plupload.setRequest(request);// 手动传入Plupload对象HttpServletRequest属性

		// 文件存储绝对路径,会是一个文件夹，项目相应Servlet容器下的"pluploadDir"文件夹 
		File dir = new File("D:/pluploadDir/");
		if (!dir.exists()) {
			dir.mkdirs();// 可创建多级目录，而mkdir()只能创建一级目录
		}
		// 开始上传文件
		pluploadService.upload(plupload, dir);
		
		Map<String, Object> map = new HashMap<>();
		map.put("data", 1);
		map.put("success", true);
		return map;

	}

}
